//
// Source code XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// (powered by Fernflower decompiler)
//

package com.jeeplus.modules.gen.service;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.BaseService;
import com.jeeplus.modules.gen.entity.GenConfig;
import com.jeeplus.modules.gen.entity.GenScheme;
import com.jeeplus.modules.gen.entity.GenTable;
import com.jeeplus.modules.gen.entity.GenTableColumn;
import com.jeeplus.modules.gen.entity.GenTemplate;
import com.jeeplus.modules.gen.mapper.GenSchemeMapper;
import com.jeeplus.modules.gen.mapper.GenTableColumnMapper;
import com.jeeplus.modules.gen.mapper.GenTableMapper;
import com.jeeplus.modules.gen.util.a;
import com.jeeplus.modules.sys.entity.Menu;
import com.jeeplus.modules.sys.service.SystemService;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(
    readOnly = true
)
public class GenSchemeService extends BaseService {
    @Autowired
    private GenSchemeMapper genSchemeMapper;
    @Autowired
    private GenTableMapper genTableMapper;
    @Autowired
    private GenTableColumnMapper genTableColumnMapper;
    @Autowired
    private SystemService systemService;

    public GenSchemeService() {
    }

    public GenScheme a(String id) {
        return (GenScheme)this.genSchemeMapper.get(id);
    }

    public Page<GenScheme> a1(Page<GenScheme> a2, GenScheme a3) {
        a.a5();
        a3.setPage(a2);
        a2.setList(this.genSchemeMapper.findList(a3));
        return a2;
    }

    @Transactional(
        readOnly = false
    )
    public String a2(GenScheme a3) throws Exception {
        String a4 = this.a3(a3);
        if(StringUtils.isBlank(a3.getId())) {
            a3.preInsert();
            this.genSchemeMapper.insert(a3);
        } else {
            a3.preUpdate();
            this.genSchemeMapper.update(a3);
        }

        return a4;
    }

    @Transactional(
        readOnly = false
    )
    public void a4(GenScheme a5, Menu a6) {
        String a7 = StringUtils.lowerCase(a5.getModuleName()) + (StringUtils.isNotBlank(a5.getSubModuleName())?":" + StringUtils.lowerCase(a5.getSubModuleName()):"") + ":" + StringUtils.uncapitalize(a5.getGenTable().getClassName());
        String a8 = "/" + StringUtils.lowerCase(a5.getModuleName()) + (StringUtils.isNotBlank(a5.getSubModuleName())?"/" + StringUtils.lowerCase(a5.getSubModuleName()):"") + "/" + StringUtils.uncapitalize(a5.getGenTable().getClassName());
        a6.setHref(a8);
        a6.setIsShow("1");
        a6.setType("1");
        a6.setPermission(a7 + ":list");
        this.systemService.saveMenu(a6);
        Menu a2 = new Menu();
        a2.setName("增加");
        a2.setIsShow("0");
        a6.setType("2");
        a2.setSort(Integer.valueOf(30));
        a2.setPermission(a7 + ":add");
        a2.setParent(a6);
        this.systemService.saveMenu(a2);
        Menu a3 = new Menu();
        a3.setName("删除");
        a3.setIsShow("0");
        a6.setType("2");
        a3.setSort(Integer.valueOf(60));
        a3.setPermission(a7 + ":del");
        a3.setParent(a6);
        this.systemService.saveMenu(a3);
        Menu a4 = new Menu();
        a4.setName("编辑");
        a4.setIsShow("0");
        a6.setType("2");
        a4.setSort(Integer.valueOf(90));
        a4.setPermission(a7 + ":edit");
        a4.setParent(a6);
        this.systemService.saveMenu(a4);
        Menu a9 = new Menu();
        a9.setName("查看");
        a9.setIsShow("0");
        a6.setType("2");
        a9.setSort(Integer.valueOf(120));
        a9.setPermission(a7 + ":view");
        a9.setParent(a6);
        this.systemService.saveMenu(a9);
        Menu b1 = new Menu();
        b1.setName("导入");
        b1.setIsShow("0");
        a6.setType("2");
        b1.setSort(Integer.valueOf(150));
        b1.setPermission(a7 + ":import");
        b1.setParent(a6);
        this.systemService.saveMenu(b1);
        Menu b2 = new Menu();
        b2.setName("导出");
        b2.setIsShow("0");
        a6.setType("2");
        b2.setSort(Integer.valueOf(180));
        b2.setPermission(a7 + ":export");
        b2.setParent(a6);
        this.systemService.saveMenu(b2);
    }

    @Transactional(
        readOnly = false
    )
    public void a5(GenScheme a1) {
        this.genSchemeMapper.delete(a1);
    }

    private String a3(GenScheme a1) throws Exception {
        StringBuilder a2 = new StringBuilder();
        GenTable a3 = (GenTable)this.genTableMapper.get(a1.getGenTable().getId());
        a3.setColumnList(this.genTableColumnMapper.findList(new GenTableColumn(new GenTable(a3.getId()))));
        GenConfig a4 = a.a7();
        List a5 = a.a8(a4, a1.getCategory(), false, a1.getFormStyle());
        List a6 = a.a8(a4, a1.getCategory(), true, a1.getFormStyle());
        if(a6.size() > 0) {
            GenTable a8 = new GenTable();
            a8.setParentTable(a3.getName());
            a3.setChildList(this.genTableMapper.findList(a8));
        }

        String a81 = "";
        Iterator a12 = a3.getChildList().iterator();

        while(a12.hasNext()) {
            GenTable a9 = (GenTable)a12.next();
            a9.setParent(a3);
            a9.setColumnList(this.genTableColumnMapper.findList(new GenTableColumn(new GenTable(a9.getId()))));
            a1.setGenTable(a9);
            Iterator tpl = a9.getColumnList().iterator();

            while(tpl.hasNext()) {
                GenTableColumn c = (GenTableColumn)tpl.next();
                if(c.getName().equals(a9.getParentTableFk())) {
                    a81 = c.getSimpleJavaField();
                }
            }

            Map tpl2 = a.a9(a1);
            tpl2.put("childUrlPrefix", tpl2.get("moduleName") + (StringUtils.isNotBlank(a1.getSubModuleName())?"/" + StringUtils.lowerCase(a1.getSubModuleName()):""));
            tpl2.put("functionName", a9.getComments());
            tpl2.put("functionNameSimple", a9.getComments());
            Iterator c1 = a6.iterator();

            while(c1.hasNext()) {
                GenTemplate tpl1 = (GenTemplate)c1.next();
                a2.append(a.a10(tpl1, tpl2, true, a1.getProjectPath()));
            }
        }

        a1.setGenTable(a3);
        Map a121 = a.a9(a1);
        a121.put("childSimpleJavaField", a81);
        Iterator a91 = a5.iterator();

        while(a91.hasNext()) {
            GenTemplate tpl3 = (GenTemplate)a91.next();
            a2.append(a.a10(tpl3, a121, true, a1.getProjectPath()));
        }

        return a2.toString();
    }

    public GenScheme b1(String a1, String a2) {
        return (GenScheme)this.genSchemeMapper.findUniqueByProperty(a1, a2);
    }
}
